package com.yang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.mapper.CheckitemMapper;
import com.yang.pojo.Checkitem;
import com.yang.pojo.PageBean;
import com.yang.pojo.R;
import com.yang.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CheckitemServiceImpl implements CheckitemService {
    @Autowired
    private CheckitemMapper checkitemMapper;

    /**
     * 条件查询检查项信息
     *
     * @param pageBean
     * @return
     */
    @Override
    public Map findPage(PageBean pageBean) {
        // 数据分页
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        // 查询数据
        List<Checkitem> list = checkitemMapper.selectList(pageBean.getQueryString());
        // 分页信息(总条数,总页数,当前页数..)
        PageInfo<Checkitem> pageInfo = new PageInfo<>(list);

        List<Checkitem> rows = pageInfo.getList();
        long total = pageInfo.getTotal();

        Map map = new HashMap();
        map.put("rows", rows);
        map.put("total", total);
        return map;
    }

    /**
     * 新增检查项
     *
     * @param checkitem
     * @return
     */
    @Override
    public R add(Checkitem checkitem) {
        R r = new R();
        Integer add = checkitemMapper.add(checkitem);
        if (add == 1) {
            r.setFlag(true);
            r.setMessage("新增检查项成功！");
        } else {
            r.setFlag(false);
        }
        return r;
    }

    /**
     * 删除检查项
     * @param id
     * @return
     */
    @Override
    public R deleteById(Integer id) {
        R r = new R();
        // 获取返回
        Integer deletegroup = checkitemMapper.deleteIdWithCheckgroup(id);
        Integer delete = checkitemMapper.delete(id);
        if (deletegroup == 1 && delete == 1) {
            r.setFlag(true);
            r.setMessage("删除成功!");
        } else {
            r.setFlag(false);
        }
        return r;
    }

    /**
     * 回显数据
     * @param id
     * @return
     */
    @Override
    public R selectById(Integer id) {
        R r = new R();
        Checkitem checkitem = checkitemMapper.selectById(id);
        r.setData(checkitem);
        r.setFlag(true);
        return r;
    }

    /**
     * 编辑检查项
     *
     * @param checkitem
     * @return
     */
    @Override
    public R update(Checkitem checkitem) {
        R r = new R();
        Integer total = checkitemMapper.update(checkitem);
        if(total == 1){
            r.setFlag(true);
            r.setMessage("编辑成功！");
        }else{
            r.setFlag(false);
        }
        return r;
    }

}
   /* //数据分页
		PageHelper.startPage(pageNum, pageSize);
    //查询数据
    List<Goods> list = gm.getGoodsList(goods);
    //分页信息（总条数、总页数、当前页数...）
    PageInfo<Goods> pageInfo = new PageInfo<Goods>(list);


    Map<String, Object> goodsMap = new HashMap<String, Object>();
		goodsMap.put("list", list);
		goodsMap.put("total", pageInfo.getTotal());
		goodsMap.put("pages", pageInfo.getPages());
		goodsMap.put("pageNum", pageInfo.getPageNum());
		goodsMap.put("pageSize", pageInfo.getPageSize());

		return goodsMap;*/